package cn.newgrand.netcall.advertisement;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jivesoftware.resource.Default;
import org.jivesoftware.spark.util.BrowserLauncher;
import org.jivesoftware.spark.util.GraphicUtils;

/**
 * 广告类的父类，主窗体广告、聊天窗体广告都继承与这个类
 * 
 * @author SidneyRen
 * 
 */
public class AdvertisementPanel extends JPanel implements UpdateAble {

	private Advertisement ad;
	private JLabel adLabel  = new JLabel();;
    private Image backgroundImage;
	
	public AdvertisementPanel(){
		setOpaque(false);
		setLayout(new GridBagLayout());
		
		adLabel.setBorder(null);
	
		adLabel.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent mouseEvent) {
				if (mouseEvent.getClickCount() == 1) {
					try {
						if (null != ad && null != ad.getURL())
							BrowserLauncher.openURL(ad.getURL());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

			public void mouseEntered(MouseEvent e) {
				adLabel.setCursor(GraphicUtils.HAND_CURSOR);
			}

			public void mouseExited(MouseEvent e) {
				adLabel.setCursor(GraphicUtils.DEFAULT_CURSOR);
			}
		});
		
	}
	
	/**
	 * 
	 * 填充广告并刷新广告面板
	 * 此方法只设置广告类实体，以及刷新advertisemPanel
	 * 
	 * 备注：并不填充adLabel，
	 * 		 具体填充adLabel的内容在子类中的fill**ad方法中进行
	 * 
	 * @param ad
	 * 
	 */
	public void fillAd(Advertisement ad){
		setAd(ad);
		invalidate();
		validateTree();
	}
	/**
	 * 获取广告标签 adLabel
	 * 
	 * @return
	 */
	public JLabel getAdLabel() {
		return adLabel;
	}

	/**
	 * 更新广告内容
	 * 
	 * @param ad
	 */
	public void updateAd(Advertisement ad) {
		// TODO Auto-generated method stub
		setAd(ad);
	}

	/**
	 * 更新广告内容
	 */
	public void updateAd() {
		ad = getAdFromServer();
		updateAd(ad);
	}

	/**
	 * 设置广告内容
	 * 
	 * @param ad
	 */
	public void setAd(Advertisement ad) {
		this.ad = ad;
	}
	/**
	 * 获取广告内容
	 * 
	 * @return
	 */
	public Advertisement getAd() {
		return ad;
	}
	/**
	 * 从服务器获取广告信息
	 * @return
	 */
	public Advertisement getAdFromServer() {
		return null;
	}

//	public void setBackgroundImage(Image image) {
//        this.backgroundImage = image;
//    }
//    public void paintComponent(Graphics g) {
//        double scaleX = getWidth() / (double) backgroundImage.getWidth(null);
//        double scaleY = getHeight() / (double) backgroundImage.getHeight(null);
//        AffineTransform xform = AffineTransform.getScaleInstance(scaleX, scaleY);
//        ((Graphics2D) g).drawImage(backgroundImage, xform, this);
//    }
	
}
